/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.gpsgame.client.util.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
public class PendingGamesDbAdapter {

    private static final String TAG = "PendingGamesDbAdapter";

	public static final String KEY_LOGIN = "login";
	public static final String KEY_GAME_ID = "gameId";
	public static final String KEY_START_DATE = "startDate";
	public static final String KEY_FINISH_DATE = "finishDate";

    private static final String DATABASE_CREATE =
        "create table if not exists PendingGames (" +
        "login text primary key, " +
        "gameId integer," +
        "startDate text," +
        "finishDate text" +
        ");";

    private static final String DATABASE_NAME = "data";
	private static final String DATABASE_TABLE = "PendingGames";
	private static final int DATABASE_VERSION = 2;

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
//        	db.execSQL("DROP TABLE IF EXISTS player");
        	db.execSQL(DATABASE_CREATE);
        }

        @Override
		public void onOpen(SQLiteDatabase db) {
//        	db.execSQL(DATABASE_CREATE);
        	db.execSQL(DATABASE_CREATE);
//        	db.execSQL("DELETE FROM player");
//        	db.execSQL("DROP TABLE IF EXISTS player");
        	super.onOpen(db);
		}

		@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS PendingGames");
            onCreate(db);
        }
    }

    public PendingGamesDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
    
    public PendingGamesDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        mDbHelper.close();
    }

    public long createPendingGame(String login, long gameId, String startDate,
    		String finishDate) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_LOGIN, login);
        initialValues.put(KEY_GAME_ID, gameId);
        initialValues.put(KEY_START_DATE, startDate);
        initialValues.put(KEY_FINISH_DATE, finishDate);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deletePendingGame(String login) {

        return mDb.delete(DATABASE_TABLE, KEY_LOGIN + " LIKE '" + login + "'", null) > 0;
    }

    public Cursor fetchPendingGame(String login) {

        Cursor mCursor =

                mDb.query(true, DATABASE_TABLE, new String[] {KEY_LOGIN, 
                		KEY_GAME_ID, 
                		KEY_START_DATE,
                		KEY_FINISH_DATE
                		}, KEY_LOGIN + " LIKE '" + login + "'", null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

//    public boolean update(Player player) {
//        ContentValues args = new ContentValues();
//        args.put(KEY_GAME_ID, player.getGameId());
//        args.put(KEY_IMAGE_ID, player.getImageId());
//        
//        return mDb.update(DATABASE_TABLE, args, KEY_LOGIN + " LIKE '" + player.getLogin() + "'", null) > 0;
//    }
//
//    public boolean setPending(String login, boolean pending) {
//        ContentValues args = new ContentValues();
//        if (pending)
//        	args.put(KEY_PENDING, 1);
//        else
//        	args.put(KEY_PENDING, 0);
//
//        return mDb.update(DATABASE_TABLE, args, KEY_LOGIN + " LIKE '" + login + "'", null) > 0;
//    }
//    
//    public boolean setGame(String login, long gameId) {
//        ContentValues args = new ContentValues();
//        args.put(KEY_GAME_ID, gameId);
//
//        return mDb.update(DATABASE_TABLE, args, KEY_LOGIN + " LIKE '" + login + "'", null) > 0;
//    }
}
